Studio 4 Extra โ€” Python Syntax & Fundamentals Review

ENGR 103 โ€” Week 4 | Duration: 1 hour 50 minutes | Format: Partner/small group

๐Ÿ“‹ Contents

0:00-0:15 - Section 1: Indentation and Colons 0:15-0:30 - Section 2: Type Casting Traps 0:30-0:45 - Section 3: f-Strings 0:45-1:05 - Section 4: Branching & Booleans 1:05-1:25 - Section 5: While Loops & Accumulators 1:25-1:50 - Section 6: Capstone Challenge

Section 1: Indentation and Colons (15 min)

Python uses indentation (spaces/tabs) to define code blocks. Every if, for, while, def, etc., must end with a colon and the body must be indented.

Exercise 1A โ€” Fix the Broken Code

Each snippet below has one or more syntax errors. Type them into your IDE and fix them so they run correctly.

Problem 1: (Expected output: B)

score = 85
if score >= 90
    print("A")
elif score >= 80:
    print("B")
else:
print("C or below")

Problem 2: (Expected output: 0, 2, 4, 6, 8)

for i in range(5):
print(i * 2)

Problem 3: (Expected output: Hello, Alex)

greet = input('would you like to greet? (Y/N)')
if greet == 'Y':
print ('Hello, Alex')

Section 2: Type Casting Traps (15 min)

CastWhat it doesCommon trap
int(x)Converts to integer (truncates)int("3.5") โ†’ ValueError
float(x)Converts to floatfloat("hello") โ†’ ValueError
str(x)Converts to string"3" + 3 โ†’ TypeError

Exercise 2A โ€” Cast Carefully

โœ๏ธ Your Task: Problem 1: The code below crashes. Fix it so that if the user enters 20, the output is Total: 21.8.
price = input("Enter price: ")
tax = price * 0.09
total = price + tax
print("Total:", total)
โœ๏ธ Your Task: Problem 2: Predict what each line prints, then verify in Python.
print(int(9.9))
print(float("42"))
print(str(100) + " dollars")
print(int(float("7.5")))

Section 3: f-Strings (15 min)

Rules: Prefix the string with f. Variables go inside {}. Format specs: {value:.2f} for 2 decimals.

Exercise 3A โ€” Format It

โœ๏ธ Your Task: Problem 1: Rewrite this print statement using an f-string.
length = 7.5
width = 3.2
area = length * width
# Old style:
print("Length: " + str(length) + ", Width: " + str(width) + ", Area: " + str(area))
โœ๏ธ Your Task: Problem 2: Use an f-string to display a receipt exactly like: 3x Coffee = $14.25
item = "Coffee"
price = 4.75
qty = 3
# TODO: print the receipt

Section 4: Branching & Booleans (20 min)

Compound logic requires the and, or, and not operators. To check if a number is even, use the modulo operator: number % 2 == 0.

Exercise 4A โ€” Number Classifier

โœ๏ธ Your Task: Write a script that asks the user for an integer. Use if/elif/else to print exactly one of the following classifications:

Section 5: While Loops & Accumulators (20 min)

Key questions for any loop: What is the starting condition? When does it stop? What updates the loop variable?

Trace Exercise

โœ๏ธ Your Task: Predict the output before running:
x = 10
while x > 0:
    print(x)
    x -= 3

Exercise 5A โ€” The Accumulator Pattern

โœ๏ธ Your Task: Write a while loop that runs exactly 3 times. Each time it loops, ask the user to input a float. Add that input to a total variable. After the loop finishes, print the average of the 3 numbers using an f-string rounded to 2 decimal places.
total = 0.0
count = 0

# TODO: Write your while loop here

Section 6: Capstone Challenge (25 min)

Let's combine everything: casting, branching, formatting, and looping.

Exercise 6A โ€” The Number Guesser

โœ๏ธ Your Task: Build a number guessing game with the following rules: